import javax.imageio.ImageIO;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.jar.JarEntry;

import static javafx.animation.Animation.Status.RUNNING;
import static javafx.scene.input.KeyCode.PAUSE;
import static javax.sound.midi.ShortMessage.START;

public class Draw extends JPanel {

    public static final int width = 400;
    public static final int height = 600;

    ArrayList<FlyingObject> flyings = new ArrayList();
    ArrayList<Bullet> bullets = new ArrayList();
    Hero hero = new Hero();

    SmallEnemy smallEnemy = new SmallEnemy();
    Bullet bullet = new Bullet();
    Bee bee = new Bee();
    BigEmeny bigEmeny = new BigEmeny();

    //定时器
    Timer timer =new Timer();
    public void action() {
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                hero.move();
                //生成子弹
                createbullet();
                //子弹移动
                bulletmove();
                //生成飞行物
                createArrayFly();
                //飞行物移动
                flymove();
                //击中判定
                hitAction();
                //越界判断
                outOf();

                //刷新显示
                repaint();
            }
        },1000,50);
        //添加监听器--适配器
        MouseAdapter adapter = new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                super.mouseExited(e);
            }

            @Override//移动
            public void mouseMoved(MouseEvent e) {
                super.mouseMoved(e);
                hero.setX(e.getX() - hero.getImageWidth() / 2);
                hero.setY(e.getY() - hero.getImageHeight() / 2);
                repaint();
            }
        };
        this.addMouseListener(adapter);
        this.addMouseMotionListener(adapter);
    }

    //子弹移动
    private void bulletmove() {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            bullet.move();
        }
    }
    //生成子弹
    int i=0;
    private void createbullet() {
        i++;
        Bullet bullet;
        if (i % 10 == 0) {
            Bullet[] bs = hero.shoot();
            for (int i = 0; i < bs.length; i++) {
                bullets.add(bs[i]);
            }
        }
    }
    //画子弹
    private void paintBullet(Graphics g) {
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            g.drawImage(bullet.getImage(),bullet.getX(),bullet.getY(),null);
        }
    }

    //飞行物移动
    private void flymove() {
        for (int i = 0; i < flyings.size(); i++) {
            FlyingObject fly = flyings.get(i);
            fly.move();
        }
    }
    //生成飞行物
    int flyingIndex=0;
    private void createArrayFly() {
        flyingIndex++;
        if(flyingIndex%20 == 0){
            int ran = (int) (Math.random()*100);
            FlyingObject fly;
            if(ran<=10){
                fly = new Bee();
            }else if(ran>10&&ran<=30){
                fly = new BigEmeny();
            }else{
                fly = new SmallEnemy();
            }
            flyings.add(fly);
        }
    }
    //画飞行物
    private void paintflyings(Graphics g) {

        for (int i = 0; i < flyings.size(); i++) {
            FlyingObject fly = flyings.get(i);
            g.drawImage(fly.getImage(),fly.getX(),fly.getY(),null);
        }
    }
    //子弹击中
    private void hitAction() {

        for (int i = 0; i < flyings.size(); i++) {
            FlyingObject fly = flyings.get(i);
            for (int j = 0; j < bullets.size(); j++) {
                Bullet bullet = bullets.get(j);
                if (fly.hit(bullet)) {
                    fly.health -= 1;
                    bullet.health -= 1;
                    //如果血量为零就消失
                    if (bullet.health == 0) {
                        bullets.remove(bullet);
                    }
                    if (fly.health == 0){
                        flyings.remove(fly);
                    }
                }
            }
        }
    }

    //判断越界
    private void outOf(){
        for (int i = 0; i < flyings.size(); i++) {
            FlyingObject fly = flyings.get(i);
            if (fly.getY() > height) {
                flyings.remove(fly);
            }
        }
    }


    public void paint(Graphics g) {
        super.paint(g);//调用父类的画画方法 - 清除运动轨迹
        //画背景
        BufferedImage bg = null;
        //加载图片
        try {
            bg = ImageIO.read(Objects.requireNonNull(Draw.class.getResourceAsStream("image/background.png")));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //画图片
        g.drawImage(bg, 0, 0, null);
        //英雄机
        g.drawImage(hero.getImage(), hero.getX(), hero.getY(), null);
        //画飞行物
        paintflyings(g);
        //画子弹
        paintBullet(g);

        /* 设置字体大小/字体颜色 */
        Font font = new Font("微软雅黑", Font.BOLD, 20);
        g.setFont(font);
        Color color = new Color(0, 0, 0);
        g.setColor(color);
        g.drawString("Score:" + hero.score, 0, 30);
        g.drawString("Life:" + hero.health, 0, 50);
    }
}

